/*
 * SimpleHelp.java
 *
 * Created on 27 de junio de 2006, 23:53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.gui.help;

import java.awt.Container;
import java.awt.Cursor;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.BoxLayout;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;

/**
 * Simple Help Frame based on JFC JEditorPane.
 * <p>
 * May someday rewrite using JavaHelp API.
 *
 * @version $Id: SimpleHelp.java,v 1.10 2003/05/30 13:16:07 ian Exp $
 */
public class SimpleHelp extends JPanel implements HyperlinkListener
{
    /** The contentpane */
    protected Container cp;
    
    /** The editorpane */
    JEditorPane help;
    
  /*
   * Construct a Help object. Just construct a JEditorPane with a URL, and it
   * does all the help from there.
   */
    public SimpleHelp (String windowName, String helpIndexFileName)
    {
        super ();
        
/*
//        cp = getContentPane ();
        getAccessibleContext ().setAccessibleName (windowName + " Help Window");
        getAccessibleContext ().setAccessibleDescription (
                "A window for viewing the help for " + windowName
                + ", which is somewhat hyperlinked.");
        */

        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        try
        {
            URL url = new File (helpIndexFileName).toURL ();

            // Only create the window once.
            if (help == null)
            {
                // System.out.println("Creat-ing help window for " + url);
            help = new JEditorPane (url);

                // System.out.println("Done!");
                help.setEditable (false);
                help.addHyperlinkListener (this);
//                JScrollPane scroller = new JScrollPane ();
//                scroller.setBorder (BorderFactory.createTitledBorder (windowName
//                        + " Help"));
//                scroller.getViewport ().add (help);
//                this.add (BorderLayout.CENTER, scroller);
                this.add (help);
//                addWindowListener (new WindowAdapter ()
//                {
//                    public void windowClosing (WindowEvent e)
//                    {
//                        SimpleHelp.this.setVisible (false);
//                        SimpleHelp.this.dispose ();
//                    }
//                });
//                setSize (500, 400);
            }
            else
            {
                System.out.println ("Re-using help window!");
            }
        }
        catch (MalformedURLException e)
        {
            System.out.println ("Malformed URL: " + e);
        }
        catch (IOException e)
        {
            System.out.println ("IOException: " + e);
        }
    }
    
    
    public void reloadPage( String helpIndexFileName )
    {
        try
        {
            URL url = new File (helpIndexFileName).toURL ();

            // Only create the window once.
//            if (help == null)
//            {
                help = new JEditorPane (url);
                help.setEditable (false);
                help.addHyperlinkListener (this);
                this.add (help);
//            }
//            else
//            {
//                System.out.println ("Re-using help window!");
//            }
        }
        catch (MalformedURLException e)
        {
            System.out.println ("Malformed URL: " + e);
        }
        catch (IOException e)
        {
            System.out.println ("IOException: " + e);
        }
    }
    
    
    
    /**
     * Notification of a change relative to a hyperlink. From:
     * java.swing.event.HyperlinkListener
     */
    public void hyperlinkUpdate (HyperlinkEvent e)
    {
        if (e.getEventType () == HyperlinkEvent.EventType.ACTIVATED)
        {
            URL target = e.getURL ();
            // System.out.println("linkto: " + target);
            
            // Get the help panel's cursor and the wait cursor
            Cursor oldCursor = help.getCursor ();
            Cursor waitCursor = Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR);
            help.setCursor (waitCursor);
            
            // Now arrange for the page to get loaded asynchronously,
            // and the cursor to be set back to what it was.
            SwingUtilities.invokeLater (new PageLoader (target, oldCursor));
        }
    }
    
    /**
     * Inner class that loads a URL synchronously into the help panel. Loads it
     * later than the request so that a cursor change can be done at the very
     * end.
     *
     * @author BORROWED FROM SUN'S SWING DEMO, UNTIL JAVAHELP AVAILABLE
     */
    class PageLoader implements Runnable
    {
        URL url;
        
        Cursor cursor;
        
        PageLoader (URL u, Cursor c)
        {
            url = u;
            cursor = c;
        }
        
        public void run ()
        {
            // System.out.println("PageLoader: u=" + url);
            if (url == null)
            {
                // restore the original cursor
                help.setCursor (cursor);
                
                // PENDING(prinz) remove this hack when
                // automatic validation is activated.
                Container parent = help.getParent ();
                parent.repaint ();
            }
            else
            {
                Document doc = help.getDocument ();
                try
                {
                    help.setPage (url);
                }
                catch (Exception ioe)
                {
                    help.setDocument (doc);
                    getToolkit ().beep ();
                }
                finally
                {
                    // schedule the cursor to revert after
                    // the paint has happended.
                    url = null;
                    SwingUtilities.invokeLater (this);
                }
            }
        }
    }
    
}